<template>
  <div class="box">
    <div class="topBar">
      <div class="topBox">
        <div>
          <el-form
            ref="headerSearchForm"
            :model="headerSearchForm"
            :inline="true"
            label-width="100px"
          >
            <el-form-item label="债券名称" prop="bondName">
              <el-input
                v-model="headerSearchForm.bondName"
                placeholder="请输入"
              >
              </el-input>
            </el-form-item>
            <el-form-item label="债券ID" prop="bondId">
              <el-input v-model="headerSearchForm.bondId" placeholder="请输入">
              </el-input>
            </el-form-item>
            <el-form-item label="债券类型" prop="bondType">
              <el-input
                v-model="headerSearchForm.bondType"
                placeholder="请输入"
              >
              </el-input>
            </el-form-item>
          </el-form>
          <div>
            <el-row>
              <el-col :span="0" style="text-align: left">
              </el-col>
              <el-col :span="24" style="text-align: right">
                <el-button
                  type="primary"
                  class="gray-btn"
                  @click="getData('search')"
                  >查询</el-button
                >
                <el-button type="primary" class="btn" @click="resetForm"
                  >重置</el-button
                >
              </el-col>
            </el-row>
          </div>
        </div>
      </div>
    </div>
    <div class="right">
      <div class="tableBox">
        <el-table :data="tableData" class="tableBoxChild">
          <el-table-column label="序号" align="center" type="index">
          </el-table-column>
          <el-table-column prop="bondId" label="债券代码" align="center">
          </el-table-column>
          <!-- <el-table-column prop="bondSname" label="债券简称" align="center">
          </el-table-column> -->
          <el-table-column prop="bondFname" label="债券名称" align="center">
          </el-table-column>
          <el-table-column prop="bondType" label="债券类型" align="center">
          </el-table-column>
          <el-table-column label="操作" align="center" width="200">
            <template slot-scope="scope">
              <!-- <span @click="editFn(scope.row)" class="operator">编辑</span>
              <span class="operator" @click="deleteFn(scope.row)">删除</span> -->
              <span @click="viewDetail(scope.row)" class="operator">详情</span>
            </template>
          </el-table-column>
        </el-table>
        <el-pagination
          background
          layout="prev, pager, next"
          :total="total"
          @current-change="pageFn"
        >
        </el-pagination>
      </div>
    </div>
    <!-- 新增编辑弹框 -->
    <el-dialog
      :title="title"
      :visible.sync="dialogVisible"
      width="1000px"
      :append-to-body="true"
      destroy-on-close
    >
      <div class="dialog-content">
        <el-form
          ref="dealMajorForm"
          :model="dealMajorForm"
          :inline="true"
          label-width="180px"
          :rules="rules"
        >
          <el-form-item label="发行人全称" prop="customerLongName">
            <el-input
              v-model="dealMajorForm.customerLongName"
              placeholder="请输入"
            >
            </el-input>
          </el-form-item>
          <el-form-item label="发行人简称" prop="customerShortName">
            <el-input
              v-model="dealMajorForm.customerShortName"
              placeholder="请输入"
            >
            </el-input>
          </el-form-item>
          <el-form-item label="发行人编码" prop="ecifCustId">
            <el-input v-model="dealMajorForm.ecifCustId" placeholder="请输入">
            </el-input>
          </el-form-item>
          <el-form-item label="公司规模" prop="comSize">
            <el-input v-model="dealMajorForm.comSize" placeholder="请输入">
            </el-input>
          </el-form-item>
          <el-form-item label="企业性质" prop="customerType">
            <el-input v-model="dealMajorForm.customerType" placeholder="请输入">
            </el-input>
          </el-form-item>
          <el-form-item label="总经理" prop="managingDirector">
            <el-input
              v-model="dealMajorForm.managingDirector"
              placeholder="请输入"
            >
            </el-input>
          </el-form-item>
          <el-form-item label="董事会秘书" prop="secretaryOfDirectors">
            <el-input
              v-model="dealMajorForm.secretaryOfDirectors"
              placeholder="请输入"
            >
            </el-input>
          </el-form-item>
          <el-form-item
            label="证券代表/法定代表人"
            prop="securitiesRepresentative"
          >
            <el-input
              v-model="dealMajorForm.securitiesRepresentative"
              placeholder="请输入"
            >
            </el-input>
          </el-form-item>
          <el-form-item label="注册地址" prop="registeredAddress">
            <el-input
              v-model="dealMajorForm.registeredAddress"
              placeholder="请输入"
            >
            </el-input>
          </el-form-item>
          <el-form-item label="联系电话" prop="contactNumber">
            <el-input
              v-model="dealMajorForm.contactNumber"
              placeholder="请输入"
            >
            </el-input>
          </el-form-item>
          <el-form-item label="公司logo">
            <el-upload
              list-type="picture"
              action=""
              accept=".jpg, .png"
              :limit="1"
              :auto-upload="false"
              :file-list="fileList"
              :on-change="getFile"
              :on-preview="handlePictureCardPreview"
              :on-remove="handleUploadRemove"
            >
              <el-button size="small" type="primary">选择图片上传</el-button>
              <div slot="tip" class="el-upload__tip" style="color: #fff">
                只能上传一张jpg/png文件
              </div>
            </el-upload>
            <el-dialog :visible.sync="dialogVisibleImage" append-to-body>
              <img width="100%" :src="dialogImageUrl" alt />
            </el-dialog>
          </el-form-item>
        </el-form>
      </div>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false" class="btn">取 消</el-button>
        <el-button type="primary" @click="handleOk" class="gray-btn"
          >确 定</el-button
        >
      </span>
    </el-dialog>
  </div>
</template>

<script>
import {
  GetBondInfoApi,
  AddBondInfoApi,
  UpdateBondInfoApi,
  DeleteBondInfoApi,
} from "@/api/customerManagement/bondRiskProfile.js";

export default {
  components: {},
  data() {
    return {
      dialogVisible: false,
      dealMajorForm: {
        customerId: "",
        customerLongName: "",
        customerShortName: "",
        customerType: "",
        managingDirector: "",
        secretaryOfDirectors: "",
        securitiesRepresentative: "",
        registeredAddress: "",
        contactNumber: "",
        companyLogo: "",
        ifIssuer: "",
      },
      ifIssuerOption: [
        {
          label: "是",
          value: "1",
        },
        {
          label: "否",
          value: "0",
        },
      ],
      rules: {
        customerLongName: [
          { required: true, message: "必填项", trigger: "blur" },
        ],
        customerShortName: [
          { required: true, message: "必填项", trigger: "blur" },
        ],
        customerType: [{ required: true, message: "必填项", trigger: "blur" }],
        ecifCustId: [{ required: true, message: "必填项", trigger: "blur" }],
        comSize: [{ required: true, message: "必填项", trigger: "blur" }],
        managingDirector: [
          { required: true, message: "必填项", trigger: "blur" },
        ],
        secretaryOfDirectors: [
          { required: true, message: "必填项", trigger: "blur" },
        ],
        securitiesRepresentative: [
          { required: true, message: "必填项", trigger: "blur" },
        ],
        registeredAddress: [
          { required: true, message: "必填项", trigger: "blur" },
        ],
        contactNumber: [{ required: true, message: "必填项", trigger: "blur" }],
        ifIssuer: [{ required: true, message: "必填项", trigger: "blur" }],
      },
      tableData: [],
      headerSearchForm: {
        bondName: "",
        bondId: "",
        bondType: "",
      },
      total: 0,
      title: "",
      pageNum: 1,
      // logo上传
      fileList: [],
      dialogImageUrl: "",
      proofImage: "",
      dialogVisibleImage: false,
    };
  },
  mounted() {},
  created() {
    this.getData();
  },
  methods: {
    // logo上传
    handleUploadRemove(file, fileList) {
      this.proofImage = "";
    },
    handlePictureCardPreview(file) {
      this.dialogImageUrl = file.url;
      this.dialogVisibleImage = true;
    },
    // 获取base64格式数据
    getFile(file, fileList) {
      this.getBase64(file.raw).then((res) => {
        const params = res.split(",");
        if (params.length > 0) {
          this.proofImage = params[1];
        }
      });
    },
    // 获取图片转base64
    getBase64(file) {
      return new Promise(function (resolve, reject) {
        const reader = new FileReader();
        let imgResult = "";
        reader.readAsDataURL(file);
        reader.onload = function () {
          imgResult = reader.result;
        };
        reader.onerror = function (error) {
          reject(error);
        };
        reader.onloadend = function () {
          resolve(imgResult);
        };
      });
    },
    async getData(type) {
      if (type) {
        this.pageNum = 1;
      }
      const params = {
        pageNum: this.pageNum,
        pageSize: 10,
        ...this.headerSearchForm,
      };
      const { data } = await GetBondInfoApi(params);
      this.tableData = data.records;
      this.total = data.total;
    },
    // 重置表单
    resetForm() {
      this.headerSearchForm = {
        customerLongName: "",
        comSize: "",
        customerType: "",
      };
      this.getData();
    },
    addFn() {
      this.title = "新增";
      this.dealMajorForm = {
        customerId: "",
        customerLongName: "",
        customerShortName: "",
        customerType: "",
        managingDirector: "",
        secretaryOfDirectors: "",
        securitiesRepresentative: "",
        registeredAddress: "",
        contactNumber: "",
        companyLogo: "",
        ifIssuer: "",
      };
      this.fileList = [];
      this.proofImage = "";
      this.dialogVisibleImage = false;
      this.dialogVisible = true;
    },
    editFn(row) {
      this.title = "编辑";
      this.dealMajorForm = JSON.parse(JSON.stringify(row));
      this.proofImage = this.dealMajorForm.companyLogo;
      this.fileList = [];
      let obj = {
        name: "logo",
        url: "data:image/png;base64," + this.proofImage,
      };
      this.fileList.push(obj);
      this.dialogVisibleImage = false;
      this.dialogVisible = true;
    },
    viewDetail(row) {
      localStorage.setItem("bondId", row.bondId);
      this.$router.push({
        name: "bondRiskProfileDetail",
        query: {
          bondId: row.bondId,
        },
      });
    },
    handleOk() {
      this.$refs["dealMajorForm"].validate(async (valid) => {
        if (valid) {
          if (this.title === "新增") {
            if (!this.proofImage) {
              this.$message.error("请上传公司Logo");
              return;
            }
            const params = {
              ...this.dealMajorForm,
              companyLogo: this.proofImage,
            };
            AddBondInfoApi(params)
              .then((res) => {
                this.$message.success("新增成功");
                this.getData();
                this.dialogVisible = false;
              })
              .catch((err) => {
                this.$message.error("新增失败");
              });
          }
          if (this.title === "编辑") {
            const params = {
              ...this.dealMajorForm,
              companyLogo: this.proofImage,
            };
            UpdateBondInfoApi(params)
              .then((res) => {
                this.$message.success("编辑成功");
                this.getData();
                this.dialogVisible = false;
              })
              .catch((err) => {
                this.$message.error("编辑失败");
              });
          }
        } else {
          return false;
        }
      });
    },
    toCustomerView() {
      this.$router.replace(
        "/customerManagement/bondRiskProfile/bondRiskProfile"
      );
    },
    pageFn(page) {
      this.pageNum = page;
      this.getData();
    },
    deleteFn(row) {
      this.$confirm("确定删除该信息?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(async () => {
          const params = {
            issuerId: row.rowNo,
          };
          await DeleteBondInfoApi(params);
          // 删除
          this.$message({
            type: "success",
            message: "删除成功!",
          });
          this.getData();
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
  },
};
</script>
<style lang="scss" scoped>
.el-table ::v-deep th.el-table__cell {
  background-color: #1c2238 !important;
  color: #fff;
}

.box {
  .topBar {
    min-height: 80px;
    background-color: #262d47;
    margin-bottom: 10px;
    padding: 20px 20px;
    padding-left: 40px;
    padding-bottom: 10px;
  }

  .topBox {
    span {
      display: inline-block;
    }

    .searchBtn {
      margin-left: 20px;
    }

    .zj {
      margin-left: 20px;
    }

    .khTitle {
      height: 30px;
      line-height: 30px;
      vertical-align: top;
      color: #ffffff;
      font-size: 14px;
      font-weight: 700px;
      margin-right: 10px;
    }

    .khInput,
    .zjInput {
      height: 30px;
      line-height: 30px;
      width: 200px;
      overflow: hidden;
      border: 1px solid rgba(126, 163, 255, 0.3);
      color: #ffffff;
      background-color: #363657;
      border-radius: 3px;
      cursor: pointer;
      text-align: center;
      line-height: 30px;
      padding: 0px 10px;
      font-size: 12px;
      white-space: nowrap;
      text-overflow: ellipsis;
    }

    .zjTitle {
      height: 30px;
      line-height: 30px;
      vertical-align: top;
      color: #ffffff;
      font-size: 14px;
      font-weight: 700px;
      margin-right: 10px;
    }
  }

  .left {
    width: 240px;
    background-color: #262d47;
    height: 100%;
  }

  .right {
    flex: 1;
    height: 88%;
    display: flex;
    flex-direction: column;
  }

  .searchList {
    padding-top: 10px;
    padding-left: 10px;
    padding-right: 10px;
    height: 700px;
    overflow: scroll;

    .searchListItem {
      height: 35px;
      line-height: 35px;
      text-align: center;
      color: #959fc5;
      font-size: 12px;
      overflow: hidden; //超出隐藏
      white-space: nowrap; //不折行
      text-overflow: ellipsis; //溢出显示省略号
    }

    .searchListItem:hover {
      background: #1f9bfd !important;
      color: #fff;
      cursor: pointer;
    }
  }

  .searchListItemAct {
    background: #1f9bfd !important;
    color: #fff !important;
  }

  .bondCard {
    background-color: #262d47;
    color: #ffffff;
    margin-bottom: 10px;

    .info {
      display: flex;
      padding: 15px 20px;

      .infoItem {
        flex: 1;
      }
    }

    .infoItemLabel {
      margin-right: 30px;
      font-size: 14px;
    }

    .infoItemVal {
      font-size: 14px;
      font-family: PingFangSC-Regular, PingFang SC;
      font-weight: 400;
      color: #d6dcff;
    }
  }

  .el-table ::v-deep th.el-table__cell {
    background-color: #1c2238 !important;
    color: #fff;
  }

  .tableBox {
    background-color: #262d47;
    flex: 1;
    padding: 15px 10px;
  }

  .tableBoxChild {
    min-height: 500px;
  }

  .el-pagination {
    text-align: right;
  }

  .leftTitle {
    height: 40px;
    line-height: 40px;
    background: #354060;
    font-size: 14px;
    font-family: PingFang-SC-Bold, PingFang-SC;
    font-weight: bold;
    color: #ffffff;
    padding-left: 17px;
    margin-bottom: 10px;
  }

  .customersCardTitle {
    height: 40px;
    background: #354060;
    font-size: 14px;
    font-family: PingFang-SC-Bold, PingFang-SC;
    font-weight: bold;
    color: #ffffff;
    padding: 10px 16px;
  }
}
.operator {
  color: #1f9bfd;
  margin-right: 8px;
  cursor: pointer;
}
::v-deep {
  .el-dialog {
    border-top: 4px solid rgba($color: #1f9bfd, $alpha: 0.9);
    .el-dialog__header {
      border-bottom: 1px solid rgba(126, 163, 255, 0.3);
      padding: 0;
      height: 49px;
      line-height: 49px;
      padding-left: 16px;
    }
    .el-dialog__footer {
      border-top: 1px solid rgba(126, 163, 255, 0.3);
    }
  }
}
</style>
